package com.hongkuncheng.cms.helper;

import com.hongkuncheng.cms.model.Admin;
import com.hongkuncheng.cms.model.AdminReAdminRole;
import com.hongkuncheng.cms.model.AdminRole;

/**
 * 用户帮助类
 *
 * @author 洪坤成
 */
public class AdminHelper {

    /**
     * 验证用户权限
     *
     * @param admin
     * @param permission
     * @return
     */
    public static boolean checkAdminPermission(Admin admin, String permission) {
        boolean result = false;
        if (admin != null) {
            if (admin.getAccount().equals("admin")) {
                return true;
            } else {
                for (AdminReAdminRole arar : admin.getAdminReAdminRoles()) {
                    if (checkAdminRolePermission(arar.getAdminRole(), permission)) {
                        result = true;
                        break;
                    }
                }
            }
        }
        return result;
    }

    /**
     * 验证角色权限
     *
     * @param adminrole
     * @param permission
     * @return
     */
    public static boolean checkAdminRolePermission(AdminRole adminrole, String permission) {
        boolean result = false;
        if (adminrole.getPermission() != null && !adminrole.getPermission().isEmpty()) {
            if (adminrole.getPermission().equals("all")) {
                result = true;
            } else {
                String[] permissionArray = adminrole.getPermission().split(",");
                for (String str : permissionArray) {
                    if (str.contains(permission)) {
                        result = true;
                        break;
                    }
                }
            }
        }
        return result;
    }
}
